package org.eae.eldf.webapp.controller.sys;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.dto.sys.SysSequenceQueryDto;
import org.eae.eldf.entity.sys.SysSequence;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.service.sys.SysSequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

/**
 * ****************************************************************************
 * @File name   :      SysSequenceController.java
 *
 * @Author      :      bbqu
 *
 * @Date        :      2016-9-1
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-9-1 上午11:19:44        bbqu     1.0            Initial Version
 ****************************************************************************
 */
@Controller
@RequestMapping("/sys")
public class SysSequenceController extends AbstractController<SysSequence>{
	
	private SysSequenceService sysSequenceService;
	@Autowired
	public void setSysSequenceService(SysSequenceService sysSequenceService) {
		this.sysSequenceService = sysSequenceService;
	}
	
	
	@RequestMapping(value="/sequence/list")
	public String listPage(ModelMap map,@RequestParam(required=false)String searchValue,HttpServletRequest request,HttpServletResponse response){
		
		return "/sys/sequence/sys_sequence";
	}
	
	@RequestMapping(value = "/sequence/list_json")
	public @ResponseBody ResultList<SysSequence> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
		@ModelAttribute(value="sysSequenceQueryDto") SysSequenceQueryDto sysSequenceQueryDto){
		Pagination<SysSequence> pager = super.newPaginationInstance(sysSequenceQueryDto);
		pager = sysSequenceService.queryListSysSequence(pager, sysSequenceQueryDto);
		return outPageArrayList(pager, sysSequenceQueryDto);
	}
	
	/**
	 * 跳转到单据页面
	 * @param modelMap
	 * @param id
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value="/sequence/form")
	@Token(save=true)
	public String toPage(ModelMap modelMap,@RequestParam(required=false) Long id,
			@RequestParam(required=false) String searchValue){
		SysSequence sysSequence=null;
		if(id!=null){
			sysSequence=sysSequenceService.querySequenceById(id);
		}
		else {
			sysSequence=new SysSequence();
		}
		modelMap.addAttribute("sysSequence",sysSequence);
		modelMap.addAttribute("searchValue",searchValue);
		return "/sys/sequence/form";

	}
	
	/**
	 * 新增单据
	 * @Date        :      2016-9-1
	 * @param modelAndView
	 * @param seqCode
	 * @param objectName
	 * @param seqFormat
	 * @param increment
	 * @param startNo
	 * @param currentNo
	 * @param prefix
	 * @param suffix
	 * @param seqLength
	 * @param currentDate
	 * @param isCycle
	 * @param seqRemark
	 * @param isReset
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping("/sequence/add")
	@ResponseBody
	/*@Token(remove=true,repeatSubmitForwardURL="/sys/sequence/form")*/
	public Result insert(ModelAndView modelAndView,
		/*	@RequestParam(required=false) String seqCode,
			@RequestParam(required=false) String objectName,
			@RequestParam(required=false) String seqFormat,
			@RequestParam(required=false) Long increment,
			@RequestParam(required=false) Long startNo,
			@RequestParam(required=false) Long currentNo,
			@RequestParam(required=false) String prefix,
			@RequestParam(required=false) String suffix,
			@RequestParam(required=false) Long seqLength,
			@RequestParam(required=false) String currentDate,
			@RequestParam(required=false) Long isCycle,
			@RequestParam(required=false) String seqRemark,
			@RequestParam(required=false) Long isReset,*/
			@ModelAttribute(value="sysSequence") SysSequence sysSequence,
			ModelMap modelMap,HttpServletRequest request){
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		if(sysSequenceService.queryCount(Criteria.create(SysSequence.class).where("seqCode", new Object[]{sysSequence.getSeqCode()}))>0){
			RequestContext context=new RequestContext(request);
			modelMap.addAttribute("errorMsg",context.getMessage("company.msg.companycode.exist"));
			modelAndView.addObject("success",false);
		}
		else {
			
			sysSequence.setSeqCode(sysSequence.getSeqCode());
			/*sysSequence.setCurrentDate(currentDate);*/
			sysSequence.setCurrentNo(sysSequence.getCurrentNo());
			sysSequence.setIncrement(sysSequence.getIncrement());
			sysSequence.setIsCycle(sysSequence.getIsCycle());
			sysSequence.setIsReset(sysSequence.getIsReset());
			sysSequence.setObjectName(sysSequence.getObjectName());
			sysSequence.setPrefix(sysSequence.getPrefix());
			sysSequence.setSeqFormat(sysSequence.getSeqFormat());
			sysSequence.setSeqLength(sysSequence.getSeqLength());
			sysSequence.setSeqRemark(sysSequence.getSeqRemark());
			sysSequence.setStartNo(sysSequence.getStartNo());
			sysSequence.setSuffix(sysSequence.getSuffix());
			sysSequenceService.insert(sysSequence);
			modelAndView.addObject("success",true);
		}
		return result;

	}

	/**
	 * 编辑单据
	 * @Date        :      2016-9-1
	 * @param id
	 * @param modelAndView
	 * @param seqCode
	 * @param objectName
	 * @param seqFormat
	 * @param increment
	 * @param startNo
	 * @param currentNo
	 * @param prefix
	 * @param suffix
	 * @param seqLength
	 * @param currentDate
	 * @param isCycle
	 * @param seqRemark
	 * @param isReset
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/sequence/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result update(@RequestParam(required=false) Long id,
			ModelAndView modelAndView,
		/*	@RequestParam(required=false) String seqCode,
			@RequestParam(required=false) String objectName,
			@RequestParam(required=false) String seqFormat,
			@RequestParam(required=false) Long increment,
			@RequestParam(required=false) Long startNo,
			@RequestParam(required=false) Long currentNo,
			@RequestParam(required=false) String prefix,
			@RequestParam(required=false) String suffix,
			@RequestParam(required=false) Long seqLength,
			@RequestParam(required=false) String currentDate,
			@RequestParam(required=false) Long isCycle,
			@RequestParam(required=false) String seqRemark,
			@RequestParam(required=false) Long isReset,*/
			@ModelAttribute(value="sysSequence") SysSequence sysSequence,
			ModelMap modelMap,HttpServletRequest request){
	/*	modelAndView.setViewName("forward:/sys/sequence/form");*/
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		sysSequenceService.update(Criteria.create(SysSequence.class)
				.set("seqCode", sysSequence.getSeqCode())
				.set("objectName", sysSequence.getObjectName())
				.set("seqFormat", sysSequence.getSeqFormat())
				.set("increment", sysSequence.getIncrement())
				.set("startNo", sysSequence.getStartNo())
				.set("currentNo", sysSequence.getCurrentNo())
				.set("prefix", sysSequence.getPrefix())
				.set("suffix", sysSequence.getSuffix())
				.set("seqLength",sysSequence.getSeqLength())
/*				.set("currentDate", currentDate)
*/				.set("isCycle", sysSequence.getIsCycle())
				.set("seqRemark", sysSequence.getSeqRemark())
				.set("isReset", sysSequence.getIsReset())
				.where("sysSequenceId", new Object[]{sysSequence.getSysSequenceId()}));
		modelAndView.addObject("success",true);
		return result;
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/sequence/del")
	@ResponseBody
	public  Result delete(
			@ModelAttribute(value="sysSequence") SysSequence sysSequence
			){
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		sysSequenceService.delSequence(sysSequence.getSysSequenceId());
		return result;
	}
	
}
